<?php
ini_set('error_reporting', 6143);
ini_set('display_errors','1');
define('IN_LC', true);
define('MM_ID', 1);

include_once("validate.php");
include_once("includes/style.php");
include_once("includes/faqs.php");
include_once("includes/addMessage.php");
include_once('includes/renderInclude.php');
include_once('includes/page_header.php');

$done = false;
$style = new style(false);	

// are we showing a catagory?
if(isset($_GET['id']))
{
	if(! isset($_SESSION['valid_admin']))
	{
		$rank = 0;
	}
	else
	{
		$rank = $_SESSION['valid_admin'];
	}
	
	// Create query
	$q = "SELECT fc_id, fc_name, fc_description
		  FROM ". FAQCATAGORIES_TABLE . "
		  WHERE fc_rank <= " . $rank . " AND fc_id = " . $_GET['id'] . "
		  GROUP BY fc_id
		  ORDER BY fc_name ASC";
		  
	// Run query
	$res = $db->sql_query($q);
	if ( $row = $db->sql_fetchrow($res)  )
	{
		$orderMessage = $lang['LicenseOrder'] . ': ';
	
		if(isset($_GET['order']))
		{
			if($_GET['order'] == 0)
			{
				$order_id = 0;
				$order = ' fq_question ASC';
				$orderMessage .= ' ' . $lang['OrderAlphabetic'] . ' | <a href="faq.php?id=' . $_GET['id'] . '&order=1">' . $lang['OrderUseful'] . '</a>';
			}
			else
			{
				$order_id = 1;
				$order = ' fq_useful DESC, fq_question ASC';
				$orderMessage .= ' <a href="faq.php?id=' . $_GET['id'] . '&order=0">' . $lang['OrderAlphabetic'] . '</a>' . ' | ' . $lang['OrderUseful'];
			}
		}
		else
		{
			$order_id = 0;
			$order = ' fq_question ASC';
			$orderMessage .= ' ' . $lang['OrderAlphabetic'] . ' | <a href="faq.php?id=' . $_GET['id'] . '&order=1">' . $lang['OrderUseful'] . '</a>';	
		}
		
		$data = Array();
		$data['logo'] = $skin_dir . 'images/faq.jpg';
		$data['title'] = '<a href="faq.php">' . $row['fc_name'] . ' - ' . $lang['FAQFull'] . '</a>';
		$data['subtitle'] = $row['fc_description'] . '<br><br>' . $lang['FAQSelectQuestion'];
		$style->addRenderLeft(new page_header($data, 'title', 'subtitle', 'logo', ''));
		$style->addRenderLeft(new addMessage($orderMessage, ''));
		
		//Add Any New Question
		
		if(isset($_SESSION['valid_id']) && isset($_POST['askQuestion']) && isset($_POST['question']) && $_POST['question'] != '')
		{
			$q = 'INSERT INTO ' . FAQS_TABLE . ' (fq_question, fq_catagory, fq_submittedby, fq_submitteddate, fq_visible)
				  VALUES ("' . $_POST['question'] . '", ' . $_GET['id'] . ', ' . $_SESSION['valid_id'] . ', NOW(), 1)';
			$db->sql_query($q);
			$style->addRenderLeft(new addMessage($lang['FAQQuestionAdded'], $lang['ChampSignedUpTitle']));
		}
		
		//Answer Any New Question
		
		if(isset($_SESSION['valid_id']) && isset($_POST['answerQuestion']) && isset($_POST['answer']) && $_POST['answer'] != '' && isset($_POST['question']) && is_numeric($_POST['question']))
		{
			$q = 'UPDATE ' . FAQS_TABLE . ' 
				  SET fq_answer = "' . $_POST['answer'] . '",
				      fq_answeredby = ' . $_SESSION['valid_id'] . ',
					  fq_answereddate = NOW() 
				  WHERE fq_id = ' . $_POST['question'];
			$db->sql_query($q);
			$style->addRenderLeft(new addMessage($lang['FAQQuestionAnswered'], $lang['ChampSignedUpTitle']));
		}
		
		$style->addRenderLeft(new renderInclude('tpl/tableStart.tpl'));
		
		$q = "SELECT fq_id, fq_question, fq_submittedby, fq_catagory, fq_answer, fq_answeredby, fq_visible, fq_useful, 
					 date_format(fq_submitteddate, '%M %e, %Y, %l:%i%p') as fq_submitteddate, 
					 date_format(fq_answereddate, '%M %e, %Y, %l:%i%p') as fq_answereddate,
					 d1.pf_drivername as fq_submitteddriver, d2.pf_drivername as fq_answereddriver 
			  FROM " . FAQS_TABLE . " 
			  LEFT JOIN " . DRIVERNAME_TABLE . " d1 ON fq_submittedby = d1.user_id
			  LEFT JOIN " . DRIVERNAME_TABLE . " d2 ON fq_answeredby = d2.user_id
			  WHERE fq_visible = 1 AND fq_catagory = " . $row['fc_id'] . "
			  ORDER BY " . $order;
		$res = $db->sql_query($q);
		while ( $faq = $db->sql_fetchrow($res)  )
		{
			$faq = formatArrayOutput($faq);
			$style->addRenderLeft(new faq_question($faq,$_GET['id'], $order_id));
		}
		$style->addRenderLeft(new renderInclude('tpl/tableEnd.tpl'));
		
		if(isset($_SESSION['valid_id']))
		{
			$style->addRenderLeft(new faq_askquestion($_GET['id'], $order_id));
		}
	}
	
	$done = true;
}

if(!$done)
{
	if(! isset($_SESSION['valid_admin']))
	{
		$rank = 0;
	}
	else
	{
		$rank = $_SESSION['valid_admin'];
	}
	
	// Create query
	$q = "SELECT fc_id, fc_name, fc_description, fc_rank, COUNT(fq_id) as fc_questioncount, COUNT(fq_answer) AS fc_answeredcount
		  FROM ". FAQCATAGORIES_TABLE . " LEFT JOIN " . FAQS_TABLE . "
		  ON fq_catagory = fc_id AND fq_visible = 1 
		  WHERE  fc_rank <= " . $rank . "
		  GROUP BY fc_id
		  ORDER BY fc_name ASC";
		  
	$data = Array();
	$data['logo'] = $skin_dir . 'images/faq.jpg';
	$data['title'] = $lang['FAQFull'];
	$data['subtitle'] = $lang['FAQSelectCatagory'];
	$style->addRenderLeft(new page_header($data, 'title', 'subtitle', 'logo', ''));
	
	$style->addRenderLeft(new renderInclude('tpl/tableStart.tpl'));
	// Run query
	$res = $db->sql_query($q);
	while ( $row = $db->sql_fetchrow($res)  )
	{
		$row = formatArrayOutput($row);
		$style->addRenderLeft(new faq_catagory($row));
	}
	$style->addRenderLeft(new renderInclude('tpl/tableEnd.tpl'));
}

$style->render();

echo $db->num_queries;
?>